/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package engine;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import xml.XMLController;

/**
 *
 * @author bi
 */
public final class CommandSpecial {

    private class CommandSpecialEach {

        public JComboBox player;
        public JComboBox special;
        public JComboBox target;

        public CommandSpecialEach() {
            player = new JComboBox(pList);
            special = new JComboBox(new String[]{Constant.COMMAND_DEFAULT});
            target = new JComboBox(new String[]{Constant.COMMAND_DEFAULT});
            player.addActionListener(playerListener);
            special.addActionListener(specialListener);
        }
    }
    private JPanel parent;
    private XMLController docFaction, docDeployment, docSpecial, docPlayer;
    private Vector<String> pList;
    private ActionListener playerListener, specialListener;
    private Vector<CommandSpecialEach> list;

    public CommandSpecial(JPanel parent,
            XMLController docPlayer,
            XMLController docFaction,
            XMLController docDeployment,
            XMLController docSpecial) {
        this.parent = parent;
        playerListener = new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                if (!list.get(list.size() - 1).player.getSelectedItem().toString().equals(Constant.COMMAND_DEFAULT)) {
                    addCommand();
                }
                removeCommand();
                addSpecialCommand((JComboBox) evt.getSource());
            }
        };
        specialListener = new ActionListener() {

            public void actionPerformed(ActionEvent evt) {
                addTargetCommand((JComboBox) evt.getSource());
            }
        };

        load(docPlayer, docFaction, docDeployment, docSpecial);
    }

    public void load(XMLController docPlayer,
            XMLController docFaction,
            XMLController docDeployment,
            XMLController docSpecial) {
        pList = new Vector<String>();
        pList.add(Constant.COMMAND_DEFAULT);
        for (int i = 0; i < docPlayer.table.length; i++) {
            if (docPlayer.table[i][3].equals("Human")) {
                pList.add(docPlayer.table[i][0]);
            }
        }

        this.docPlayer = docPlayer;
        this.docFaction = docFaction;
        this.docDeployment = docDeployment;
        this.docSpecial = docSpecial;

        parent.removeAll();
        list = new Vector<CommandSpecialEach>();
        addCommand();
    }

    public void addCommand() {
        CommandSpecialEach cse = new CommandSpecialEach();
        list.add(cse);
        parent.add(cse.player);
        parent.add(cse.special);
        parent.add(cse.target);
        parent.setLayout(new GridLayout(list.size(), 3, 50, 30));
    }

    public void removeCommand() {
        for (int c = 0; c < list.size() - 1; c++) {
            CommandSpecialEach cse = list.get(c);
            if (list.get(c).player.getSelectedItem().toString().equals(Constant.COMMAND_DEFAULT)) {
                list.remove(cse);
                parent.remove(cse.player);
                parent.remove(cse.special);
                parent.remove(cse.target);
                parent.setLayout(new GridLayout(list.size(), 3, 50, 30));
                c--;
            }
        }
    }

    public void addSpecialCommand(JComboBox source) {
        for (int c = 0; c < list.size(); c++) {
            CommandSpecialEach cse = list.get(c);
            if (cse.player.equals(source)) {
                String playerName = cse.player.getSelectedItem().toString();

                Vector<String> squadName = new Vector<String>();
                for (int i = 0; i < docDeployment.table.length; i++) {
                    if (docDeployment.table[i][0].equals(playerName)) {
                        if (!squadName.contains(docDeployment.table[i][1])) {
                            squadName.add(docDeployment.table[i][1]);
                        }
                    }
                }

                Vector<String> specialName = new Vector<String>();
                specialName.add(Constant.COMMAND_DEFAULT);
                for (int i = 0; i < docSpecial.table.length; i++) {
                    for (int j = 0; j < squadName.size(); j++) {
                        if (docSpecial.table[i][1].equals(squadName.get(j))) {
                            specialName.add(docSpecial.table[i][0]);
                        }
                    }
                }

                cse.special.setModel(new DefaultComboBoxModel(specialName));
            }
        }
    }

    public void addTargetCommand(JComboBox source) {
        for (int c = 0; c < list.size(); c++) {
            CommandSpecialEach cse = list.get(c);
            if (cse.player.equals(source)) {
                String playerName = cse.player.getSelectedItem().toString();
                String specialName = cse.special.getSelectedItem().toString();
                String factionName = "";
                String teamName = "";

                for (int i = 0; i < docPlayer.table.length && factionName.equals(""); i++) {
                    if (docPlayer.table[i][0].equals(playerName)) {
                        factionName = docPlayer.table[i][1];
                    }
                }
                for (int i = 0; i < docFaction.table.length && teamName.equals(""); i++) {
                    if (docFaction.table[i][2].equals(factionName)) {
                        teamName = docFaction.table[i][1];
                    }
                }

                boolean targetA = false;
                for (int i = 0; i < docSpecial.table.length && !targetA; i++) {
                    if (docSpecial.table[i][0].equals(specialName)) {
                        if (docSpecial.table[i][3].equals("Allied")) {
                            targetA = true;
                        }
                    }
                }

                Vector<String> targetList = new Vector<String>();

                targetList.add(Constant.COMMAND_DEFAULT);
                if (targetA) {
                    targetList.add(teamName);
                } else {
                    for (int i = 0; i < docFaction.table.length; i++) {
                        if (!docFaction.table[i][1].equals(teamName)
                                && !targetList.contains(docFaction.table[i][1])) {
                            targetList.add(docFaction.table[i][1]);
                        }
                    }
                }

                cse.target.setModel(new DefaultComboBoxModel(targetList));
            }
        }
    }

    public String[][] getCommand() {
        String[][] commandList = new String[list.size() - 1][3];
        for (int i = 0; i < list.size() - 1; i++) {
            commandList[i][0] = list.get(i).player.getSelectedItem().toString();
            commandList[i][1] = list.get(i).special.getSelectedItem().toString();
            commandList[i][2] = list.get(i).target.getSelectedItem().toString();
        }
        return commandList;
    }
}
